package com.hnkywl.sts.service.xsgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.xsgl.JfmxDao;
import com.hnkywl.sts.dao.xsgl.XsmxDao;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Melody_for_love
 * Date: 13-7-16
 * Time: 下午3:34
 * To change this template use File | Settings | File Templates.
 */
@Service
public class XsmxService {

    private XsmxDao xsmxDao;
    private JfmxDao jfmxDao;

    //销售明细列表显示
    public String findAllXsmx(Long ckId, String xsdh, String xslx, String djlx, Long sbgsId, Long sbbmId, Long khId, String ggName, String ggCode, String ggXh, Date fhrqStart, Date fhrqEnd, String zt, String jszt, Long gcId, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page xsmxPage = this.xsmxDao.findXsmxList(ckId, xsdh, xslx, djlx, sbgsId, sbbmId, khId, ggName, ggCode, ggXh, fhrqStart, fhrqEnd, zt, jszt, gcId, pageNo, pageSize);
        String xsmxJson = gson.toJson(xsmxPage.getResult());
        List<Map> sumList = this.xsmxDao.findSumXsmx(ckId, xsdh, xslx, djlx, sbgsId, sbbmId, khId, ggName, ggCode, ggXh, fhrqStart, fhrqEnd, zt, jszt, gcId);
        Double sbsl = 0d;
        Double ck_sbsl = 0d;
        Double hse = 0d;
        Double bhse = 0d;
        Double xscbzh = 0d;
        Double xslr = 0d;
        if (sumList.size() != 0) {
            java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
            java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.00");
            for (Map map : sumList) {
                sbsl += Double.valueOf((map.get("sbsl") == null ? 0d : map.get("sbsl")).toString());
                ck_sbsl += Double.valueOf((map.get("ck_sbsl") == null ? 0d : map.get("ck_sbsl")).toString());
                hse += Double.valueOf((map.get("hse") == null ? 0d : map.get("hse")).toString());
                bhse += Double.valueOf((map.get("bhse") == null ? 0d : map.get("bhse")).toString());
                xscbzh += Double.valueOf((map.get("xscbzh") == null ? 0d : map.get("xscbzh")).toString());
                xslr += Double.valueOf((map.get("xslr") == null ? 0d : map.get("xslr")).toString());
            }
            return "{\"total\":" + xsmxPage.getTotalCount() + ",\"rows\":" + xsmxJson + ",\"footer\":[{\"xsdh\":\"合计:\",\"khmc_jc\":\"数量\",\"wzgg_oldcode\":" + df.format(sbsl) + ",\"wzgg_code\":\"出库数量\",\"wzgg_name\":" + df.format(ck_sbsl) + ",\"wzgg_ggxh\":\"含税总金额(元)\",\"kh_cd\":" + jtdf.format(hse) + ", \"wzgg_dw\":\"不含税总金额(元)\", \"sbsl\":" + jtdf.format(bhse) + ",\"xscbzh\":" + jtdf.format(xscbzh) + ",\"xslr\":" + jtdf.format(xslr) + " }]}";
        } else {
            return "{\"total\":" + xsmxPage.getTotalCount() + ",\"rows\":" + xsmxJson + "}";
        }
    }

    //赎货明细报表
    public String shmxList(Long gsId, Long gysId, Long khId, String cght, String xsd, Date start, Date end, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Map<String, Object> map = xsmxDao.shmxList(gsId, gysId, khId, cght, xsd, start, end, pageNo, pageSize);
        return "{\"total\":" + map.get("count") + ",\"rows\":" + gson.toJson(map.get("rows")) + ",\"footer\":" + new Gson().toJson(map.get("footer")) + "}";
    }

    public String findSskcList(String xszxh, String carNo, String bindNo, String type, Long xshtId, int pageNo, int pageSize) {
        Gson gson = new Gson();
        Page<Map> page = xsmxDao.findSskcList(xszxh, carNo, bindNo, type, xshtId, pageNo, pageSize);
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }

    public String findXsmxJfmx(String xsdh, String xszxh, String carNo, String bindNo, Long xshtId, int pageNo, int pageSize) {
        Gson gson = new Gson();
        Page<Map> page = jfmxDao.findJfmxList(xsdh, xszxh, carNo, bindNo, xshtId, pageNo, pageSize);
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }

    public List<Map> findXsmxExpList(String xsdh, String xszxh, String carNo, String bindNo, Long xshtId, int pageNo, int pageSize) {
        Page<Map> xsmxPage = this.jfmxDao.findJfmxList(xsdh, xszxh, carNo, bindNo, xshtId, pageNo, pageSize);
        return xsmxPage.getResult();
    }

    @Autowired
    public void setXsmxDao(XsmxDao xsmxDao) {
        this.xsmxDao = xsmxDao;
    }

    @Autowired
    public void setJfmxDao(JfmxDao jfmxDao) {
        this.jfmxDao = jfmxDao;
    }
}
